/**
 * 
 */
package xj.toolkit.bean.tlv.test;

import java.util.Map;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import xj.toolkit.bean.tlv.decode.decoders.BeanTLVDecoder;
import xj.toolkit.bean.tlv.encode.encoders.BeanTLVEncoder;
import xj.toolkit.util.ByteUtils;


/**
 * @author hp
 *
 */
public class TlvDemo2 {

    public static void main(String[] args) {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
            	"xj/toolkit/bean/tlv/test/tlvDemo2.xml");
    	
    	BeanTLVEncoder beanEncoder = (BeanTLVEncoder)ctx.getBean("beanEncoder");
    	
    	SampleTLV sample = new SampleTLV();
    	sample.setAge(101);
    	sample.setHeight(1.063f);
    	sample.setWeight(135.08765678);
    	sample.setName("hello, world!");
    	sample.setChildNum((byte)0x5);
    	
    	Map<String, String> childs = Maps.newHashMap();
    	childs.put("xiaojian", "xiaojian");
    	
    	sample.setChilds(childs);
    	
    	
    	ChildTLV tlv1 = new ChildTLV();
    	tlv1.setNum(1);
    	tlv1.setNum2(2);
    	
    	ChildTLV tlv2 = new ChildTLV();
    	tlv2.setNum(3);
    	tlv2.setNum2(4);
    	
    	sample.setChildren(Lists.newArrayList(tlv1, tlv2));
    	
    	byte[]	bytes = ByteUtils.union(beanEncoder.encode(sample, 
    			beanEncoder.getEncodeContextFactory().createEncodeContext(SampleTLV.class, null)));
    	
    	System.out.println( "TLV bytes is :\n" + ByteUtils.bytesAsHexString(bytes, 1024) );
    	
    	BeanTLVDecoder beanDecoder = (BeanTLVDecoder)ctx.getBean("beanDecoder");
    	Object bean = beanDecoder.decode(bytes.length, bytes, 
    			beanDecoder.getDecodeContextFactory().createDecodeContext(SampleTLV.class, null));
    	System.out.println( bean );

    }
}
